import message from '@/utils/message'

export function formatData(data, origin, children) {
  data.map((item, index) => {
    if (item.type == 'dept') {
      formatData(item.children, origin, children)
    } else if (item.type == 'carrier') {
      if (item.value == origin.value) {
        item.children = children
      }
    }
  })
  return data;
}
export function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) { return pair[1]; }
  }
  return (false);
}

export function dataFormat(data, fmt) { //author: meizz
  let o = {
    "M+": data.getMonth() + 1,                 //月份
    "d+": data.getDate(),                    //日
    "h+": data.getHours(),                   //小时
    "m+": data.getMinutes(),                 //分
    "s+": data.getSeconds(),                 //秒
    "q+": Math.floor((data.getMonth() + 3) / 3), //季度
    "S": data.getMilliseconds()             //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (data.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (let k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
//制造级联数据   参数分别为 {lable 名称 value 名称 children 名称},数据集
//注意事项 ： 此函数必须每级的数据key名称一致
export function markCascadeList({ lable_name, value_name, children_name }, source) {
  console.log(lable_name);
  const getThreeList = (mark_list) => {
    const return_list = [];
    mark_list.map((item) => {
      return_list.push({
        ...item,
        key: item[value_name],
        title: item[lable_name],
        children: item[children_name] && item[children_name].length > 0 ? getThreeList(item[children_name]) : undefined,

      })
    })
    return return_list;
  }
  try {
    if (source && source.length > 0) {
      return getThreeList(source);
    } else {
      console.error("utle.js markCascadeList() err !使用此函数数据源必须为数组类型");
    }
  } catch (e) {
    console.log(e);
  }

}
//制造级联数据   参数分别为 {lable 名称 value 名称 children 名称},数据集
//注意事项 ： 此函数必须每级的数据key名称一致
export function markCascadeList2({ lable_name, value_name, children_name }, source) {
  const getThreeList = (mark_list) => {
    const return_list = [];
    mark_list.map((item) => {
      return_list.push({
        value: item[value_name],
        label: item[lable_name],
        children: item[children_name] && item[children_name].length > 0 ? getThreeList(item[children_name]) : undefined
      })
    })
    return return_list;
  }
  try {
    if (source && source.length > 0) {
      return getThreeList(source);
    } else {
      console.error("utle.js markCascadeList() err !使用此函数数据源必须为数组类型");
    }
  } catch (e) {
    console.log(e);
  }

}
export function getIndexUuid() {
  const arr = [];
  const hexDigits = "0123456789abcdef";
  for (let i = 0; i < 36; i++) {
    arr[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  arr[0] = "zb-";
  arr[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  arr[19] = hexDigits.substr((arr[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  arr[8] = arr[13] = arr[18] = arr[23] = "-";
  const uuid = arr.join("");
  return uuid;
}

//参数数组points的每一项为每一个点的：纬度,经度
//如 points = ['30.86660,104.390740', '30.861961,104.386963', '30.842287,104.388079', '点的纬度,点的经度'……];
//返回中心点的数组[纬度,精度]
export function getPointsCenter(points) {
  var point_num = points.length; //坐标点个数
  var X = 0, Y = 0, Z = 0;
  for (let i = 0; i < points.length; i++) {
    if (points[i] == '') {
      continue;
    }
    let point = points[i].split(',');
    var lat, lng, x, y, z;
    lat = parseFloat(point[0]) * Math.PI / 180;
    lng = parseFloat(point[1]) * Math.PI / 180;
    x = Math.cos(lat) * Math.cos(lng);
    y = Math.cos(lat) * Math.sin(lng);
    z = Math.sin(lat);
    X += x;
    Y += y;
    Z += z;
  }
  X = X / point_num;
  Y = Y / point_num;
  Z = Z / point_num;

  var tmp_lng = Math.atan2(Y, X);
  var tmp_lat = Math.atan2(Z, Math.sqrt(X * X + Y * Y));

  return [tmp_lat * 180 / Math.PI, tmp_lng * 180 / Math.PI];
}

export function slicePx(str) {
  return Number(str.replace('px', ''))
}

// const slicePx = (str) => {
//     return Number(str.replace('px', ''))
// }

export function getStyle(dom, type) {
  return Math.floor(slicePx(getComputedStyle(dom)[type]))
}
